﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BookStore.Bussiness;
using BookStore.Data;
namespace BookStore.admin
{
    public partial class subCatogory : System.Web.UI.Page
    {
        static bool flag = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["Administrator"])))
            {
                Response.Redirect("~/admin/Login.aspx");
            }
            showAll();
            if (!Page.IsPostBack)
            {
               loadDDL(); 
            }
        }

        public void showAll()
        {
          gvSubCatogory.DataSource=subCatogoryService.getAll();
            gvSubCatogory.DataBind();
        }
        public void ResetControl() {
            txtSubCatogoryID.Text = "";
            txtSubCatogoryName.Text = "";
        }
        public void loadDDL()
        {
            ddlCatogoryID.DataSource = catogoryService.getAll();
            ddlCatogoryID.DataTextField = "catogryName";
            ddlCatogoryID.DataValueField = "catogoryID";
            ddlCatogoryID.DataBind();
        }

        public void insertSubCatogory()
        {
            subCatogoryInfo objSubcatogory= new subCatogoryInfo();
            //if (subCatogoryService.checkExist(txtSubCatogoryID.Text)==true)
            //{
            //    Response.Write("<script language='javascript'>alert('subCatogoryID existed');</script>");
            //    return;
            //}
            //objSubcatogory.subCatogoryID = Convert.ToInt32(txtSubCatogoryID.Text);
            objSubcatogory.subCatogoryName = txtSubCatogoryName.Text;
            objSubcatogory.catogoryID = Convert.ToInt32(ddlCatogoryID.SelectedValue);
            bool test = subCatogoryService.insertSubCatogory(objSubcatogory);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('insert success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('insert failed');</script>");
            }
        }
        public void updateSubcatogory()
        {
            subCatogoryInfo objSubcatogory = new subCatogoryInfo();
            objSubcatogory.subCatogoryID =Convert.ToInt32(txtSubCatogoryID.Text);
            objSubcatogory.subCatogoryName =txtSubCatogoryName.Text;
            objSubcatogory.catogoryID =Convert.ToInt32(ddlCatogoryID.SelectedValue) ;
            
            bool test = subCatogoryService.updateSubCatogory(objSubcatogory);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('update success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('update failed');</script>");
            }
        }
    
     

        protected void lbtUpdateT_Click(object sender, EventArgs e)
        {
            if (flag == true)
            {
                insertSubCatogory();
            }
            else
            {
                updateSubcatogory();
            }
            showAll();
            pnUpdate.Visible = false;
            pnView.Visible = true;
        }

        protected void lbtAddT_Click(object sender, EventArgs e)
        {
            flag = true;
            ResetControl();
            pnView.Visible = false;
            pnUpdate.Visible = true;
        }

        protected void lbtRefreshT_Click(object sender, EventArgs e)
        {
            showAll();
        }

        protected void gvSubCatogory_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int SubcatogoryID =Convert.ToInt32(gvSubCatogory.Rows[e.RowIndex].Cells[0].Text);
            if (subCatogoryService.checksubCatogoryID(SubcatogoryID)==true)
            {
                Response.Write("<script language='javascript'>alert('product still existed , dont delete');</script>");
                return;
            }

            bool test = subCatogoryService.deleteSubCatogory(SubcatogoryID);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('delete success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('delete failed');</script>");
            }
            showAll();
        }

        protected void gvSubCatogory_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            flag = false;
            int SubCatogoryID = Convert.ToInt32(gvSubCatogory.Rows[e.NewSelectedIndex].Cells[0].Text);
            subCatogoryInfo objSubCatogory = subCatogoryService.getbyID(SubCatogoryID);
            txtSubCatogoryID.Text = objSubCatogory.subCatogoryID.ToString();
            txtSubCatogoryName.Text = objSubCatogory.subCatogoryName;
            ddlCatogoryID.SelectedValue = objSubCatogory.catogoryID.ToString();
            pnUpdate.Visible = true;
            pnView.Visible = false;
        }
    }
}
